import { Engine } from "../engine";  
import { Vector2 } from "../vector";  
import { Enemy, EnemyType, EnemyState } from "./enemy";  
  
export class WalkerEnemy extends Enemy {  
  constructor(engine: Engine, pos: Vector2) {  
    super(engine, pos, EnemyType.WALKER);  
    this.speed = 1.0;  
    this.health = 50;  
    this.detectionRange = 100;  
    this.attackRange = 25;  
  }  
  
  update_(): void {  
    if (this.isDead) return;  
  
    this.physics.update_();  
    this.updateAI();  
    this.updateAnimation();  
  }  
  
  private updateAI(): void {  
    const now = this.engine.time_;  
      
    switch (this.state) {  
      case EnemyState.IDLE:  
        if (this.canSeePlayer()) {  
          this.state = EnemyState.CHASE;  
          this.lastStateChange = now;  
        } else if (this.patrolPoints.length > 0) {  
          this.state = EnemyState.PATROL;  
          this.lastStateChange = now;  
        }  
        break;  
  
      case EnemyState.PATROL:  
        if (this.canSeePlayer()) {  
          this.state = EnemyState.CHASE;  
          this.lastStateChange = now;  
        } else {  
          this.patrol();  
        }  
        break;  
  
      case EnemyState.CHASE:  
        const distance = this.getDistanceToPlayer();  
          
        if (!this.canSeePlayer()) {  
          this.state = EnemyState.PATROL;  
          this.lastStateChange = now;  
        } else if (distance <= this.attackRange) {  
          this.state = EnemyState.ATTACK;  
          this.lastStateChange = now;  
        } else {  
          this.moveTowards(this.engine.player.body_.pos);  
        }  
        break;  
  
      case EnemyState.ATTACK:  
        this.attackPlayer();  
          
        if (this.getDistanceToPlayer() > this.attackRange) {  
          this.state = EnemyState.CHASE;  
          this.lastStateChange = now;  
        }  
        break;  
    }  
  }  
  
  private updateAnimation(): void {  
    this.animation_.frame = (this.animation_.frame + 1) % 60;  
      
    if (Math.abs(this.body_.vel.x) > 0.1) {  
      this.animation_.scale = 1 + Math.sin(this.animation_.frame / 10) * 0.1;  
    } else {  
      this.animation_.scale = 1;  
    }  
  }  
  
  render(ctx: CanvasRenderingContext2D): void {  
    if (this.isDead) return;  
  
    ctx.save();  
      
    ctx.translate(this.body_.pos.x, this.body_.pos.y);  
    ctx.scale(this.animation_.direction === "l" ? -1 : 1, this.animation_.scale);  
      
    // 绘制敌人身体（简单的红色矩形）  
    ctx.fillStyle = this.state === EnemyState.CHASE ? "#ff4444" : "#cc2222";  
    ctx.fillRect(-8, -16, 16, 16);  
      
    // 绘制眼睛  
    ctx.fillStyle = "#ffffff";  
    ctx.fillRect(-6, -12, 3, 3);  
    ctx.fillRect(3, -12, 3, 3);  
      
    // 绘制血条  
    if (this.health < this.maxHealth) {  
      const barWidth = 20;  
      const barHeight = 3;  
      const healthPercent = this.health / this.maxHealth;  
        
      ctx.fillStyle = "#000000";  
      ctx.fillRect(-barWidth/2, -25, barWidth, barHeight);  
        
      ctx.fillStyle = "#ff0000";  
      ctx.fillRect(-barWidth/2, -25, barWidth * healthPercent, barHeight);  
    }  
      
    ctx.restore();  
  }  
}